﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Collections;
using System.Collections.ObjectModel;
using System.Xml;
using System.Xml.XPath;

using log4net;

using ConfigUtilLib;
using CommonLib;

namespace GmailFetcher
{

        public class EmailAccount : FetcherConfigBaseRec
        {
            public const string IMAP = "IMAP";
            private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
            private const string hostStr = "host";
            private const string userStr = "user";
            private const string nameAttrString = "name";
            private const string typeAttrStr = "type";
            private const string emailTypeStr = "emailType";
            private const string activeStr = "active";
            private const string passwdStr = "passwd";
            private const string folderStr = "Folder";


            public string host { get; private set; }
            public string userName { get; private set; }
            public string type { get; private set; }
            public string passwd { get; private set; }
            public bool isValidConfig { get; private set; }
            public string EmailFolder { get; private set; }

            public bool LoadConfiguration(XmlNode fetcherNode)
            {
                isValidConfig = true;
                
                XmlAttribute xmlAttr;
                #region READ_BASICS

                // read fetcher Name Attribute 
                xmlAttr = fetcherNode.Attributes[nameAttrString];
                if (xmlAttr == null)
                {
                    log.DebugFormat("AttributeMissing : " + nameAttrString + " :  For fetcher Record ");
                    return false;
                }
                _FetcherName = xmlAttr.Value;

                log.DebugFormat("Name: " + FetcherName);


                xmlAttr = fetcherNode.Attributes[typeAttrStr];
                if (xmlAttr == null)
                {
                    log.FatalFormat("AttributeMissing : " + typeAttrStr + " :  For fetcher Record ");
                    return false;
                }

                _FetcherType = xmlAttr.Value;

                #endregion

                #region READ_HOSTNAME
                // Read Host Name 
                xmlAttr = fetcherNode.Attributes[hostStr];
                if (xmlAttr == null)
                {
                    isValidConfig = false;
                    log.DebugFormat("Unable to Get HostName from Account Config ");
                }
                else
                {
                    host = xmlAttr.Value;
                }
                #endregion

                #region READ_USER_NAME
                // Read user Name
                xmlAttr = fetcherNode.Attributes[userStr];
                if (xmlAttr == null)
                {
                    isValidConfig = false;
                    log.DebugFormat("Unable to Get User Name  from Account Config ");
                }
                else
                {
                    userName = xmlAttr.Value;
                }
                #endregion

                #region _READ_TYPE
                // Read type of Email ( POP/IMAP)
                xmlAttr = fetcherNode.Attributes[emailTypeStr];
                if (xmlAttr == null)
                {
                    isValidConfig = false;
                    log.DebugFormat("Unable to Get Type  Name  from Account Config ");
                }
                else
                {
                    type = xmlAttr.Value;
                }
                #endregion

                #region FOLDER 
                EmailFolder = "INBOX";
                xmlAttr = fetcherNode.Attributes[folderStr];
                if (xmlAttr == null)
                {
                    isValidConfig = false;
                    log.DebugFormat("Unable to Get Type  Name  from Account Config ");
                }
                else
                {
                    EmailFolder = xmlAttr.Value;
                }
                #endregion


                #region _READ_PASSWD
                // Get password 
                xmlAttr = fetcherNode.Attributes[passwdStr];
                if (xmlAttr == null)
                {
                    isValidConfig = false;
                    log.DebugFormat("Unable to Get Password String from Account Config ");
                }
                else
                {
                    passwd = xmlAttr.Value;
                }
                #endregion

                #region READ_ACTIVE_FLAG
                // Get Active /inactive 
                _isActive = true;

                xmlAttr = fetcherNode.Attributes[activeStr];
                if (xmlAttr == null)
                {
                    isValidConfig = false;
                    log.DebugFormat("Unable to Get User Name  from Account Config ");
                }
                else
                {
                    _isActive = System.Convert.ToBoolean(xmlAttr.Value);
                }

                #endregion

                log.DebugFormat("Completed Reading Record for Mail Account Configuration Section..");
                return isValidConfig;
            }


        }

    }
